<template>
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('homepage.Package.packageBox')" />
		<!-- 无查询数据状态 -->
		<zy-empty v-if="this.cardinfo.length==0"></zy-empty>
		<!-- 查询结果列表 -->
		<cardInfo :cardinfo="cardinfo" v-else></cardInfo>
		<!-- 底部按钮 -->
		<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('public.submit')" @rightBtn="submitBtn" @leftBtn="resetBtn"
			:leftDisabled="disabled" :rightDisabled="disabled">
		</bottomBtn>
	</view>
</template>
<script>
	import {
		getBox,
		PackingReport
	} from "@/api/homepage.js"
	import cardInfo from './components/infoList.vue'
	export default {
		components: {
			cardInfo,
		},
		data() {
			return {
				cardinfo: [],
				disabled: true,
			}
		},
		onLoad() {
			//PDA扫描
			var _this = this
			uni.$on('scancodedate', function(data) {
				_this.searchList(data.code.replace(/\s/g, ""))
			})
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
		},
		watch:{
			cardinfo:{
				handler(n,o){
					if(n.length>0){
						this.disabled=false
					}else{
						this.disabled=true
					}
				},
				immediate:true,
				deep:true,
			}
		},
		methods: {
			searchList(e) {
				getBox({
					boxNo: e,
					status: 0
				}).then((res) => {
					if (res.code == 0) {
						if (this.cardinfo.length == 0) {
							this.cardinfo = res.data
						} else {
							if (this.cardinfo.findIndex(item => item.code === this.boxNo) !== -1) {
								uni.showToast({
									title: this.$t('public.searchRepeat'),
									icon: "none",
									duration: 3000
								})
							} else {
								this.cardinfo.push(newGoods)
							}
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/Package/index"
				})
			},
			submitBtn: function() {
				const detailDatas = [];
				for (var i = 0; i < this.cardinfo.length; i++) {
					const temp = this.cardinfo[i].id
					detailDatas.push(temp);
				};
				PackingReport({
					packingProcedureId: '',
					boxIds: detailDatas,
				}).then((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
					uni.redirectTo({
						url: "/pages/HomePage/Package/index"
					})
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
		}
	}
</script>
<style lang="scss" scoped>
</style>